package HoneWork;
interface Good {
    public String getName();
    public double getPrice();
}

class Book implements Good {
    private String name;
    private double price;

    public Book(String name,double price) {
        this.setName(name);
        this.setPrice(price);
    }

    @Override
    public String getName() {
        return name;
    }
    @Override
    public double getPrice() {
        return price;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

class Drink implements Good {
    private String name;
    private double price;

    public Drink(String name,double price) {
        this.setName(name);
        this.setPrice(price);
    }
    @Override
    public String getName() {
        return name;
    }
    @Override
    public double getPrice() {
        return price;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

class ShopCar {
    private Good[] goods;
    private int foot;

    public ShopCar() {

    }
    //创建数组开辟购物车的大小
    public ShopCar(int len) {
        if(len > 0) {
            this.goods = new Good[len];
        }
        else {
            this.goods = new Good[1];
        }
    }
    //增加购物车物品
    public boolean Add(Good good) {
        if(foot<this.goods.length) {
            this.goods[foot] = good;
            foot++;
            System.out.println("成功添加");
            return true;
        }
        else {
            System.out.println("添加失败");
            return false;
        }
    }
    public void remove(int index) {
        if(index<=0&&index>=goods.length) {
            System.out.println("输入的位置有误");
        }
        else {
            for(int i = index - 1;i < goods.length-1;i++) {
                goods[i] = goods[i+1];
            }
            foot--;
        }
        System.out.println("成功删除了位置:" + index);
    }
    public double Buy() {
        double sum = 0;
        for(Good good:goods) {
            sum+=good.getPrice();
        }
        return sum;
    }

}


public class Test_2 {
    public static void main(String[] args) {
        ShopCar gs = new ShopCar(4);

        gs.Add(new Book("语文书",1));
        gs.Add(new Book("数学书",2));
        gs.Add(new Book("英语书",3));
        gs.remove(2);
        gs.Add(new Drink("奶茶",4));
        gs.Add(new Drink("冰红茶",5));

        System.out.println("商品总金额为->"+gs.Buy());
    }
}
